/******************************************************************************
 * * watch_msg_handle.c - implementation of UI internal message handler
 *
 * *(C) Copyright 2019 Asr International Ltd.
 * * All Rights Reserved
 * ******************************************************************************/
#include "lv_watch.h"

#if USE_LV_WATCH_MODEM_ADAPTOR

static lv_task_t * msg_monitor_task = NULL;

void UI_Send_Inter_Msg(UI_SUB_MODULE_ID Dest, UI_INTER_MSG_ID MsgId, UI_Inter_Msg_t * Msg)
{
    UI_Os_Msg_t * message = (UI_Os_Msg_t *)lv_mem_alloc(sizeof(UI_Os_Msg_t));

    message->MsgHeader.MsgId = MsgId;
    message->pMsg = Msg;

    switch(Dest) {
        case UI_SUB_MODULE_WATCH_UI:
            printf("~~~~~send message %d to gui\n", MsgId);
            if(NULL != gui_task_id) {
                Hal_Send_Message((TASK_ID)gui_task_id, message);
            } else {
                Hal_Mem_Free(message);
                printf("$$$$$$$$$$$gui_task_id is NULL\n");
            }
            break;
        case UI_SUB_MODULE_MODEM_ADP:
            printf("~~~~~send message %d to modem\n", MsgId);
            if(NULL != modem_adp_task_id) {
                Hal_Send_Message((TASK_ID)modem_adp_task_id, message);
            } else {
                Hal_Mem_Free(message);
                printf("$$$$$$$$$$$modem_adp_task_id is NULL\n");
            }

            break;
        case UI_SUB_MODULE_APP_ADP:
            // todo: Hal_Send_Message(, &message);
            break;
        default:
            break;
    }
}

void watch_ui_message_handle(uint16_t msgid, void * msg)
{
    switch(msgid) {
        case MMI_MODEM_POWER_UP_CNF:
            watch_modem_power_up_cnf();
            break;

        case MMI_MODEM_POWER_OFF_CNF:
            watch_modem_power_off_cnf();
            break;

        case MMI_MODEM_CALL_RESPONSE_ORIGINATING:
            watch_modem_call_response_originating(((mmi_msg_call_response_originating_t *)msg)->index);
            break;

        case MMI_MODEM_CALL_RESPONSE_ALERTING:
            watch_modem_call_response_alerting(((mmi_msg_call_response_alerting_t *)msg)->index);
            break;

        case MMI_MODEM_CALL_RESPONSE_RING:
            watch_modem_call_response_ring(((mmi_msg_call_response_ring_t *)msg)->index);
            break;

        case MMI_MODEM_CALL_RESPONSE_CONNECT:
            watch_modem_call_response_connect(((mmi_msg_call_response_connect_t *)msg)->index);
            break;

        case MMI_MODEM_CALL_RESPONSE_DISCONNECT:
            watch_modem_call_response_disconnect(((mmi_msg_call_response_disconnect_t *)msg)->index);
            break;

        case MMI_MODEM_CALL_RESPONSE_NO_CARRIER:
            watch_modem_call_response_no_carrier(((mmi_msg_call_response_no_carrier_t *)msg)->index, ((mmi_msg_call_response_no_carrier_t *)msg)->isNoCarrier);
            break;

        case MMI_MODEM_CALL_RESPONSE_DIAL:
            watch_modem_call_response_dial(((mmi_msg_call_response_dial_t *)msg)->isOk);
            break;

        case MMI_MODEM_CALL_RESPONSE_CLIP:
            watch_modem_call_response_clip(((mmi_msg_call_response_clip_t *)msg)->index,
                                           ((mmi_msg_call_response_clip_t *)msg)->clip);
            break;

        case MMI_MODEM_CALL_RESPONSE_STATE_CHANGE:
            watch_modem_call_response_state_change(((mmi_msg_call_response_state_change_t *)msg)->state);
            break;

        case MMI_MODEM_CALL_RESPONSE_CEER:
            watch_modem_call_response_ceer(((mmi_msg_call_response_ceer_t *)msg)->ceer);
            break;

        case MMI_MODEM_SIGNAL_BAR_IND:
            watch_modem_signal_bar_ind(((mmi_msg_signal_bar_ind_t *)msg)->bar);
            break;

        case MMI_MODEM_OPERATOR_IND:
            watch_modem_operator_ind(((mmi_msg_operator_ind_t *)msg)->rat);
            lv_mem_free(((mmi_msg_operator_ind_t *)msg)->Operator);
            break;

        case MMI_MODEM_TIME_UPDATE_IND:
            watch_modem_time_update_ind(((mmi_msg_time_update_ind_t *)msg)->TimeInfo);
            break;

        case MMI_MODEM_SIM_STATUS_IND:
            lv_mem_free(((mmi_msg_sim_status_ind_t *)msg)->SimStatus);
            //watch_modem_sim_status_ind(((mmi_msg_sim_status_ind_t *)msg)->SimStatus);
            break;

        case MMI_MODEM_SEND_SMS_CNF:
            watch_modem_send_sms_cnf(((mmi_msg_send_sms_cnf_t *)msg)->psendsmscnf);
            break;
        case MMI_MODEM_READ_SMS_DETAIL_ON_SIM_CNF:
            watch_modem_read_sms_detail_on_sim_cnf(((mmi_msg_read_sms_detail_on_sim_cnf_t *)msg)->preadsmscnf);
            break;

        case MMI_MODEM_REP_NEW_SMS_IND:
            watch_modem_rep_new_sms_ind(((mmi_msg_rep_new_sms_ind_t *)msg)->prepnewsms);
            break;
#if USE_LV_WATCH_ENGINEER_MODE != 0
        case MMI_MODEM_PIN_STATUS_CHANGE_IND:
            watch_modem_pin_status_change_ind(((mmi_msg_pin_status_change_ind_t *)msg)->pinreqtype);
            break;
#endif

#if USE_LV_WATCH_ENGINEER_MODE!= 0
        case MMI_MODEM_LTE_CELLS_INFO:
            watch_modem_lte_cells_info_process(((mmi_msg_lte_cells_info_t *)msg)->pltecells);
            break;

        case MMI_MODEM_GSM_CELLS_INFO:
            watch_modem_gsm_cells_info_process(((mmi_msg_gsm_cells_info_t *)msg)->pgsmcells);
            break;

        case MMI_MODEM_CALI_RESULT_IND:
            watch_modem_calibration_result_process(((mmi_msg_cali_rslt_ind_t *)msg)->result);
            break;

        case MMI_MODEM_SN_INFO_IND:
            watch_modem_sn_info_process(((mmi_msg_sn_info_ind_t *)msg)->psn);
            break;
#endif
        case MMI_MODEM_CALL_RESPONSE_CCWA:
            watch_modem_call_response_ccwa(((mmi_msg_call_response_ccwa_t *)msg)->index,
                                           ((mmi_msg_call_response_ccwa_t *)msg)->ccwa);
            break;

        case MMI_MODEM_CALL_RESPONSE_REMOTE_HOLD:
        case MMI_MODEM_PLMN_SEARCH_CNF:
        case MMI_MODEM_CALL_FORWARD_SET_CNF:
        case MMI_MODEM_CALL_FORWARD_QUERY_CNF:
        case MMI_MODEM_CALL_WAIT_SET_CNF:
        case MMI_MODEM_CALL_WAIT_QUERY_CNF:
        case MMI_MODEM_GET_PHONEBOOK_STATUS_CNF:
        case MMI_MODEM_WRITE_PHONEBOOK_CNF:
        case MMI_MODEM_READ_PHONEBOOK_CNF:
        case MMI_MODEM_PHONEBOOK_INIT_IND:
        case MMI_MODEM_DELETE_SMS_CNF:
        case MMI_MODEM_READ_SMS_INFO_CNF:
        case MMI_MODEM_GET_SMSC_CNF:
        case MMI_MODEM_SET_SMSC_CNF:
        case MMI_MODEM_SET_DELIVERY_REP_CNF:
        case MMI_MODEM_SMS_STATUS_REP_IND:
        case MMI_MODEM_SIM_SMS_STORAGE_STATUS_IND:
        case MMI_MODEM_SEND_USSD_CNF:
            break;

        case MMI_HAL_LOW_POWER_IND:
            watch_low_power_ind_proc(((mmi_msg_low_power_ind_t *)msg)->bat_percent);
            break;

        case MMI_APP_ADAPTOR_VOICE_MSG_RCV_IND:
            app_adaptor_voice_msg_handle_rcv_ind(((mmi_msg_app_adaptor_voice_msg_rcv_ind_t *)msg)->msg);
            break;

        case MMI_APP_ADAPTOR_VOICE_MSG_SEND_CNF:
            app_adaptor_voice_msg_handle_send_cnf(((mmi_msg_app_adaptor_voice_msg_send_cnf_t *)msg)->result);
            break;

#if USE_LV_WATCH_UPGRADE != 0
        case MMI_FOTA_CHECK_VERSION_IND:
            upgrade_check_version_ind_handler(((mmi_msg_fota_check_version_ind_t *)msg)->version);
            break;

        case MMI_FOTA_DOWNLOAD_PERCENT_IND:
            upgrade_download_percent_ind_handler(((mmi_msg_fota_download_percent_ind_t *)msg)->percent);
            break;

        case MMI_MODEM_SN_INFO_IND:
            watch_modem_sn_info_process(((mmi_msg_sn_info_ind_t *)msg)->psn);
            break;

        case MMI_FOTA_RESULT_IND:
            upgrade_result_ind_handler(((mmi_msg_fota_result_ind_t *)msg)->result);
            break;
#endif

        case MMI_MODEM_IMS_VOPS_IND:
            watch_modem_ims_vops_ind(((mmi_msg_ims_vops_ind_t *)msg)->display_sendsms_flg);
            break;

#if USE_LV_WATCH_VIDEOCALL != 0
        case MMI_MODEM_VIDEO_CALL_RESPONSE_INCOMING:
        case MMI_MODEM_VIDEO_CALL_RESPONSE_RING:
        case MMI_MODEM_VIDEO_CALL_RESPONSE_CONNECT:
        case MMI_MODEM_VIDEO_CALL_RESPONSE_DISCONNECT:
        case MMI_MODEM_VIDEO_CALL_RESPONSE_BOTTOM_ERR:
        case MMI_MODEM_VIDEO_CALL_RESPONSE_NEED_REDIAL:
        case MMI_MODEM_VIDEO_CALL_RESPONSE_AUTO_TEST:
            watch_ui_voip_message_handle(msg);
            break;
        case MMI_MODEM_VIDEO_CALL_RESPONSE_TEST_STOP:
        case MMI_MODEM_VIDEO_CALL_RESPONSE_TEST_VOICE_MO_REQ:
        case MMI_MODEM_VIDEO_CALL_RESPONSE_TEST_MT_CALL:
        case MMI_MODEM_VIDEO_CALL_RESPONSE_TEST_VIDEO_MO_REQ:
        case MMI_MODEM_VIDEO_CALL_RESPONSE_TEST_REJ_CALL:
            watch_voip_autotest_handle(msg);
            break;
#endif
#if USE_LV_BLUETOOTH != 0
        case MMI_BT_SWITCH_ON_CNF:
        case MMI_BT_SWITCH_OFF_CNF:
            printf("ui handle bt swtich msg\n");
            watch_bt_switch_message_handle(msg);
            break;
        case MMI_BT_INQUIRY_CNF:
            printf("ui handle bt inquiry msg\n");
            watch_bt_inquiry_message_handle(msg);
            break;
        case MMI_BT_PAIR_CNF:
            printf("ui handle bt pair msg\n");
            watch_bt_paired_message_handle(msg);
            break;
        case MMI_BT_ACL_CONNECT_CNF:
            printf("ui_handle bt acl connect msg\n");
            watch_bt_acl_connect_message_handle(msg);
            break;

        case MMI_BT_AVRCP_KEY_CNF:
            printf("ui_handle avrcp key msg\n");
            #if USE_LV_WATCH_AUDIO_PLAYER != 0
            watch_bt_avrcp_connect_message_handle(msg);
            #endif
            break;
        case MMI_BT_HEADSET_CONNECT_CNF:
            watch_bt_connect_message_handle(msg);
            break;

        //BT HFP profile msg begin
        case MMI_BT_HFP_CONNECTED:
            printf("ui_handle bt hfp CONNECTED msg\n");
            watch_bt_hfp_connected_message_handle();
            break;
        case MMI_BT_HFP_DISCONNECTED:
            printf("ui_handle bt hfp DISCONNECTED msg\n");
            watch_bt_hfp_disconnect_message_handle();
            break;
        case MMI_BT_HFP_ATA:
            printf("ui_handle bt hfp ATA msg\n");
            watch_bt_hfp_ata_message_handle();
            break;
        case MMI_BT_HFP_ATCHUP:
            printf("ui_handle bt hfp ATCHUP msg\n");
            watch_bt_hfp_atchup_message_handle();
            break;
        case MMI_BT_HFP_DIAL:
            printf("ui_handle bt hfp DIAL msg\n");
            watch_bt_hfp_dial_message_handle();
            break;
        case MMI_BT_HFP_ATCHLD:
            printf("ui_handle bt hfp ATCHLD msg\n");
            watch_bt_hfp_atchld_message_handle();
            break;
        case MMI_BT_HFP_VOICE_RECOGNITION:
            printf("ui_handle bt hfp RECOGNITION msg\n");
            watch_bt_hfp_recognition_message_handle(msg);
            break;
        case MMI_BT_HFP_SPEAKER_VOLUME:
            printf("ui_handle bt hfp SPEAKER VOLUME msg\n");
            watch_bt_hfp_speaker_volume_message_handle(msg);
            break;
        case MMI_BT_HFP_MICROPHONE_GAIN:
            printf("ui_handle bt hfp MICROPHONE GAIN msg\n");
            watch_bt_hfp_microphone_gain_message_handle(msg);
            break;

        //BT HFP profile msg end
        case MMI_BT_OBEX_FILE_RECEIVE_START:
        case MMI_BT_OBEX_FILE_RECEIVING:
        case MMI_BT_OBEX_FILE_RECEIVE_FINISH:
        case MMI_BT_OBEX_FILE_RECEIVE_ABORT:
        case MMI_BT_OBEX_FILE_RECEIVE_START_AND_FINISH:
        case MMI_BT_OBEX_FILE_SEND_FAILED:
        case MMI_BT_OBEX_FILE_SEND_SUCCESS:
        case MMI_BT_OBEX_FILE_SEND_START:
        case MMI_BT_OBEX_FILE_SEND_CONTINUE:
            printf("ui_handle bt obex msg\n");
            watch_bt_obex_message_handle(msg);
            break;
        case MMI_BT_SCO_DISCONNECTED:
            printf("ui_handle bt sco disconnect msg\n");
            watch_bt_sco_disconnetct_message_handle();
            break;
#endif
# if USE_LV_WATCH_QQ != 0
        case MMI_QQ_QUERY_QR_CNF:
            watch_qq_query_qr_cnf_handle(msg);
            break;
        case MMI_QQ_GET_QR_CNF:
            watch_qq_get_qr_cnf_handle(msg);
            break;
        case MMI_QQ_REGISTER_CNF:
            watch_qq_register_with_openid_cnf_handle(msg);
            break;
        case MMI_QQ_GET_MSG_CNF:
            watch_qq_get_msg_cnf_handle(msg);
            break;
        case MMI_QQ_SEND_MSG_CNF:
            watch_qq_send_msg_cnf_handle(msg);
            break;
        case MMI_QQ_GET_FRIEND_MSG_CNF:
            watch_qq_get_friend_msg_cnf_handle(msg);
            break;
        case MMI_QQ_GET_FRIEND_LIST_CNF:
            watch_qq_get_friend_list_cnf_handle(msg);
            break;
        case MMI_QQ_GET_FRIEND_INFO_CNF:
            watch_qq_get_friend_info_cnf_handle(msg);
            break;
        case MMI_QQ_GET_RECENT_CONTACT_CNF:
            watch_qq_get_recent_contact_cnf_handle(msg);
            break;
        case MMI_QQ_FIND_FRIEND_CNF:
            watch_qq_find_friend_cnf_handle(msg);
            break;
        case MMI_QQ_ADD_FRIEND_CNF:
            watch_qq_add_friend_cnf_handle(msg);
            break;
        case MMI_QQ_ACK_ADD_FRIEND_CNF:
            watch_qq_ack_add_friend_cnf_handle(msg);
            break;
        case MMI_QQ_DELETE_FRIEND_CNF:
            watch_qq_delete_friend_cnf_handle(msg);
            break;
        case MMI_QQ_GET_UNDECIDED_CNF:
            watch_qq_get_undecided_cnf_handle(msg);
            break;
        case MMI_QQ_GET_DATA_CNF:
            watch_qq_get_data_cnf_handle(msg);
            break;
        case MMI_QQ_MSG_READ_CNF:
            break;
        case MMI_QQ_NEW_MSG_NOTIFY:
            watch_qq_new_message_notify_handle(msg);
            break;
#endif
        default:
            break;
    } /* switch */
} /* watch_ui_message_handle */

void watch_message_handle_entry(lv_task_t * task)
{
    (void)task;
    UI_Inter_Msg_Header_t * msg = NULL;

    if(NULL == gui_task_id) return;

    while(1) {
        msg = (UI_Inter_Msg_Header_t *)Hal_Receive_Message((TASK_ID)gui_task_id, false);
        if(NULL != msg) {
            printf("gui msg %d received\n", msg->MsgId);
            watch_ui_message_handle(msg->MsgId, (void *)msg);
            lv_mem_free(msg);
        } else {
            return;
        }
    }
}

void watch_message_handle_init(void)
{
    if(NULL != msg_monitor_task) {
        lv_task_del(msg_monitor_task);
    }

    msg_monitor_task = lv_task_create(watch_message_handle_entry, 50, LV_TASK_PRIO_HIGHEST, NULL);
}

void watch_message_handle_entry_for_charging(lv_task_t * task)
{
    (void)task;
    UI_Inter_Msg_Header_t * msg = NULL;

    if(NULL == gui_task_id) return;

    while(1) {
        msg = (UI_Inter_Msg_Header_t *)Hal_Receive_Message((TASK_ID)gui_task_id, false);
        if(NULL != msg) {
            printf("gui msg %d received\n", msg->MsgId);
            if(MMI_MODEM_PIN_STATUS_CHANGE_IND == msg->MsgId) {
                watch_set_pintype(((mmi_msg_pin_status_change_ind_t *)msg)->pinreqtype);
            }
            lv_mem_free(msg);
        } else {
            return;
        }
    }
}

void watch_message_handle_init_for_charging(void)
{
    msg_monitor_task = lv_task_create(watch_message_handle_entry_for_charging, 50, LV_TASK_PRIO_HIGHEST, NULL);
}

void watch_message_handle_destroy(void)
{
    lv_task_del(msg_monitor_task);
}

#endif
